<template>
  <el-container>
    <el-header>
      <div class="header">
        <div class="title">
          <a
            style="color: black; text-decoration: none"
            href="/src/pages/teacherHome/home.html"
            >&lt;返回 |</a
          >
          个人中心
        </div>
        <div class="user-info">
          <div class="notification">
            <i class="bell-icon">🔔</i>
            <span class="badge">1</span>
          </div>
          <div class="user">Hello, {{ userStore.uname }}!</div>
          <div class="actions">
            <a href="./login.html">退出</a>
          </div>
        </div>
      </div>
    </el-header>

    <el-main>
      <div class="content-wrapper">
        <div class="tab-menu">
          <span
            ref="profileTab"
            :class="{ active: activeTab === 'profile' }"
            @click="selectTab('profile')"
            >个人信息</span
          >
          <span
            ref="settingsTab"
            :class="{ active: activeTab === 'settings' }"
            @click="selectTab('settings')"
            >设置</span
          >
          <div class="underline" :style="underlineStyle"></div>
        </div>

        <div v-if="activeTab === 'profile'" class="profile-section container">
          <el-text class="mx-1" size="large">姓名</el-text>
          <el-input
            v-model="userStore.uname"
            style="width: 240px"
            placeholder="请输入用户名"
          />

          <el-text class="mx-1" size="large">手机号</el-text>
          <el-input
            v-model="userStore.phone"
            style="width: 240px"
            placeholder="请输入手机号"
          />

          <el-text class="mx-1" size="large">邮箱</el-text>
          <el-input
            v-model="userStore.email"
            style="width: 240px"
            placeholder="请输入个人邮箱"
          />

          <div></div>
          <el-button type="primary" @click="saveUserInfo"
            >保存个人信息</el-button
          >
        </div>
        <div v-if="activeTab === 'settings'" class="settings-section container">
          <h1>修改密码</h1>
          <div></div>
          <el-text class="mx-1" size="large">旧密码</el-text>
          <el-input
            v-model="oldPwd"
            style="width: 240px"
            placeholder="请输入旧密码"
          />
          <el-text class="mx-1" size="large">新密码</el-text>
          <el-input
            v-model="newPwd"
            type="password"
            style="width: 240px"
            placeholder="请输入新密码"
          />
          <el-text class="mx-1" size="large">确认新密码</el-text>
          <el-input
            v-model="repeatNewPwd"
            type="password"
            style="width: 240px"
            placeholder="请确认新密码"
          />
          <div></div>
          <el-button type="primary" @click="changePwd">确认修改</el-button>
        </div>
      </div>
    </el-main>
  </el-container>
</template>

<script setup>
import { useUserStore } from "../../store/user.js";
import { ref, computed, nextTick } from "vue";
import "element-plus/dist/index.css";
import axios from "axios";

let userStore = useUserStore();
userStore.storeInit();

const activeTab = ref("profile");
const profileTab = ref(null);
const settingsTab = ref(null);
const oldPwd = ref("");
const newPwd = ref("");
const repeatNewPwd = ref("");

const underlineStyle = computed(() => {
  const tabElement =
    activeTab.value === "profile" ? profileTab.value : settingsTab.value;
  return {
    width: `${tabElement ? tabElement.offsetWidth : 0}px`,
    transform: `translateX(${tabElement ? tabElement.offsetLeft : 0}px)`,
  };
});

function selectTab(tab) {
  activeTab.value = tab;
  nextTick(() => {
    const tabElement = tab === "profile" ? profileTab.value : settingsTab.value;
    if (tabElement) {
      tabElement.scrollIntoView({ behavior: "smooth", inline: "center" });
    }
  });
}

function saveUserInfo() {
  userStore.setUname(userStore.uname);
  userStore.setPhone(userStore.phone);
  userStore.setEmail(userStore.email);
}

function changePwd() {
  // axios({
  //   url: "http://localhost:9090/changePwd",
  //   method: "POST",
  //   data: {
  //     uid: userStore.uid,
  //     oldPwd: oldPwd.value,
  //     newPwd: newPwd.value,
  //   },
  // })
  //   .then((response) => {
  //     console.log(response.data);
  //   })
  //   .catch((err) => {
  //     console.log(err);
  //   });

  axios({
    url: "http://localhost:9090/changePwd",
    method: "POST",
    params: {
      uid: userStore.uid,
      oldPwd: oldPwd.value,
      newPwd: newPwd.value,
    },
  })
    .then((response) => {
      // console.log(response.data);
      window.location.href = "/index.html"
    })
    .catch((err) => {
      console.log(err);
    });
}
</script>

<style scoped>
.container {
  display: grid;
  grid-template-columns: 100px 200px;
  row-gap: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background-color: #fff;
  border-bottom: 1px solid #ddd;
}

.header .title {
  font-size: 24px;
  color: #1e90ff;
}

.header .user-info {
  display: flex;
  align-items: center;
}

.header .user-info .notification {
  margin-right: 20px;
  position: relative;
}

.header .user-info .notification .badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background-color: red;
  color: #fff;
  border-radius: 50%;
  padding: 2px 5px;
  font-size: 12px;
}

.header .user-info .user {
  margin-right: 10px;
  color: #333;
}

.header .user-info .actions a {
  margin: 0 5px;
  color: #1e90ff;
  text-decoration: none;
}

.content-wrapper {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.tab-menu {
  display: flex;
  justify-content: start;
  margin-bottom: 20px;
  position: relative;
}

.tab-menu span {
  padding: 10px 20px;
  cursor: pointer;
  font-weight: bold;
}

.tab-menu span.active {
  color: #1e90ff;
}

.tab-menu .underline {
  position: absolute;
  bottom: 0;
  height: 2px;
  background-color: #1e90ff;
  transition: transform 0.3s ease;
}
</style>
